<template>
  <!-- template 模板的意思 -->
  <!-- 用插槽站位  写上名字就是具名插槽 防止多个插槽混乱 -->
  <!-- <template #body="{row}"> -->
  <!-- @blur="row.inputVisible=fales" 失去焦点 为什么要等于一个空 -->
  <div>
    <MyTable :arr="list">
      <template #head>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>

      <template #body="{ row }">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>

        <td>
          <input
            class="tag-input form-control"
            style="width: 100px"
            type="text"
            v-model="row.nputValue"
            v-if="row.inputVisible"
            v-focus
            @blur="row.inputVisible = fales"
            @keydown.enter="addfn(row)"
            @keydown.esc="row.nputValue=''"
          />
          <button
            v-else
            style="display: block"
            class="btn btn-primary btn-sm add-tag"
            @click="row.inputVisible = true"
          >
            +Tag
          </button>

          <span
            v-for="(item, index) in row.tags"
            :key="index"
            class="badge badge-warning"
          >
            {{ item }}
          </span>
        </td>

        <td>
          <button class="btn btn-danger btn-sm" @click="spliceFn(row.id)">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    };
  },
  created() {
    this.$axios({
      url: "/api/goods",
    })
      .then((res) => {
        this.list = res.data.data;
        console.log(res);
      })
      .catch((err) => console.log(err));
  },
  methods: {
    spliceFn(id) {
      //传过来的是数的id
      //filter 是过滤筛选的意思
      // this.list=this.list.filter(item=>item.id!==id)//filter 就能删除呢
      let index = this.list.findIndex((item) => item.id === id); //找索引删除
      this.list.splice(index, 1);
    },
    addfn(row) {
      if (row.nputValue.trim().length === 0) {
       alert("亲爱呢 不能输入为空")
        return 
      }
      row.tags.push(row.nputValue);
      row.nputValue=''
    },
  },
};
</script>